import java.util.Deque;
import java.util.LinkedList;

public class num958 {
    public boolean isCompleteTree(TreeNode root) {
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        boolean issecond = false;
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (!issecond) {
                if (cur.left != null && cur.right != null) {
                    queue.offer(cur.left);
                    queue.offer(cur.right);
                } else if (cur.left != null) {
                    queue.offer(cur.left);
                    issecond = true;
                } else if (cur.right != null) {
                    return false;
                } else {
                    issecond = true;
                }
            }else{
                if(cur.left != null || cur.right != null){
                    return false;
                }
            }
        }
        return true;
    }
}
